package testcase;

import connectionpool.C3P0Executor;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import static java.lang.Thread.sleep;

/**
 * 简单测试连接池的使用
 */
public class C3P0PoolTest {
    
    private final static String url = "jdbc:opengauss://192.168.0.243:14100,192.168.0.243:14200,192.168.0.243:14300/test?autoBalance=leastconn";
    
    private final static String username = "test";
    
    private final static String password = "huawei123.";
    
    private final static int MAX_IDLE = 1000;
    
    private final static int MAX_TOTAL = 1000;
    
    C3P0Executor executor = new C3P0Executor(url, username, password);
    
    private void testCreateConnection() throws SQLException, InterruptedException {
        initConnectionPool(MAX_IDLE, MAX_TOTAL);
        initTable();
        int time = 0;
        List<Connection> connections = new ArrayList<>();
        for (int i = 0; i < MAX_TOTAL; i++) {
            connections.add(executor.getConnection());
        }
    }
    
    private void testCreateConnectionShutdownNode() throws SQLException, InterruptedException {
        initConnectionPool(MAX_IDLE, MAX_TOTAL);
        initTable();
        int time = 3000;
        List<Connection> connections = new ArrayList<>();
        while (time-- > 0) {
            Thread.sleep(100);
            connections.add(executor.getConnection());
            connections.add(executor.getConnection());
//            connections.add(executor.getConnection());
        }
    }
    
    private void testExecute() {
        int time = 0;
        try {
            sleep(2000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        while (true) {
            try {
                System.err.println("client: time=" + time++);
                sleep(900);
                Connection connection = executor.getConnection();
                System.err.println("Connection is valid: " + connection.isValid(100));
                executor.executeSQL("select * from t1;");
                executor.executeSQL("select * from t2;");
                System.err.println("Connection is valid: " + connection.isValid(100));
                connection.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    
    private void initConnectionPool() throws SQLException {
        executor = new C3P0Executor(url, username, password);
    }
    
    private void initConnectionPool(int minIdle, int maxSize) throws SQLException {
        executor = new C3P0Executor(url, username, password, minIdle, maxSize);
    }
    
    private void initTable() throws SQLException {
        Connection connection = executor.getConnection();
        Statement statement = connection.createStatement();
        statement.execute("drop table if exists t1");
        statement.execute("drop table if exists t2");
        statement.execute("create table t1 (id int, value1 int, value2 int)");
        statement.execute("create table t2 (id int, value1 int, value2 int)");
        statement.execute("insert into t1 values (1, 100, 1000)");
        statement.execute("insert into t1 values (1, 100, 1000)");
        statement.execute("insert into t1 values (1, 100, 1000)");
        statement.execute("insert into t1 values (1, 100, 1000)");
        statement.execute("insert into t1 values (1, 100, 1000)");
        statement.execute("insert into t2 values (2, 100, 1000)");
        statement.execute("insert into t2 values (2, 100, 4000)");
        statement.execute("insert into t2 values (2, 300, 1000)");
        statement.execute("insert into t2 values (2, 100, 2000)");
        statement.execute("insert into t2 values (2, 200, 1000)");
        connection.close();
    }
    
    public static void main(String[] args) throws SQLException, InterruptedException {
//        new C3P0PoolTest().testCreateConnection();
        new C3P0PoolTest().testCreateConnectionShutdownNode();
    }
}
